package com.law.lawcaculator.controller;

import com.law.lawcaculator.model.dto.GenerateReportResponse;
import com.law.lawcaculator.model.dto.SubmitAnswersRequest;
import com.law.lawcaculator.model.entity.SubmitAnswers;
import com.law.lawcaculator.service.AdvancedReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/report")
@RequiredArgsConstructor
@Tag(name = "报告管理", description = "报告生成和下载相关接口")
public class ReportController {

    private final AdvancedReportService advancedReportService;

    /**
     * 生成详细报告
     * @param request 包含会话ID、答案列表和应用ID的请求对象
     * @return 包含报告内容的响应
     */
    @PostMapping("/generate")
    @Operation(summary = "生成详细报告", description = "根据用户提交的答案生成详细的法律计算报告")
    public ResponseEntity<GenerateReportResponse> generateReport(
            @Parameter(description = "报告生成请求对象", required = true) @RequestBody SubmitAnswersRequest request) {
        
        GenerateReportResponse response = advancedReportService.generateDetailedReport(
                request.getSessionId(), 
                request.getAnswers(), 
                String.valueOf(request.getAppId())
        );
        return ResponseEntity.ok(response);
    }

    /**
     * 下载Word格式报告
     * @param sessionId 会话ID
     * @param response HttpServletResponse用于输出文件
     */
    @GetMapping("/download/{sessionId}")
    @Operation(summary = "下载Word报告", description = "下载指定会话ID的Word格式报告")
    public void downloadReport(
            @Parameter(description = "会话ID", required = true) @PathVariable String sessionId,
            HttpServletResponse response) {
        
        advancedReportService.downloadWordReport(sessionId, response);
    }
}